package com.hy.usercenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hy.usercenter.common.ResultEnum;
import com.hy.usercenter.enums.TeamStatusEnum;
import com.hy.usercenter.exception.CommonException;
import com.hy.usercenter.model.domain.SysUser;
import com.hy.usercenter.model.domain.Team;
import com.hy.usercenter.model.domain.UserTeamRelation;
import com.hy.usercenter.model.dto.JoinTeamParam;
import com.hy.usercenter.service.SysUserService;
import com.hy.usercenter.service.TeamService;
import com.hy.usercenter.service.UserTeamRelationService;
import com.hy.usercenter.mapper.UserTeamRelationMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author a
* @description 针对表【user_team_relation(用户和队伍的关系表)】的数据库操作Service实现
* @createDate 2023-02-26 15:09:57
*/
@Service
public class UserTeamRelationServiceImpl extends ServiceImpl<UserTeamRelationMapper, UserTeamRelation>
    implements UserTeamRelationService{

    @Autowired
    private TeamService teamService;

    @Override
    public Boolean joinToTeam(JoinTeamParam joinTeamParam) {

        Long teamId = joinTeamParam.getTeamId();
        Team team = teamService.getById(teamId);
        Long userId = joinTeamParam.getUserId();


        LambdaQueryWrapper<UserTeamRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserTeamRelation::getUserId, userId);
        queryWrapper.eq(UserTeamRelation::getTeamId, teamId);
        Long count = baseMapper.selectCount(queryWrapper);

        Integer maxNum = team.getMaxNum();
        if (maxNum < (count+1)) {
            throw new CommonException(ResultEnum.PARAMS_ERROR, "队伍数到达上限");
        }
        Long teamUserId = team.getUserId();
        if (userId.equals(teamUserId)) {
            throw new CommonException(ResultEnum.PARAMS_ERROR, "用户不能加入自己的队伍");
        }
        Date expireTime = team.getExpireTime();
        if (expireTime != null && new Date().after(expireTime)) {
            throw new CommonException(ResultEnum.PARAMS_ERROR, "队伍已过期");
        }
        // 不能重复加入 已经加入的队伍
        if (count > 0) {
            throw new CommonException(ResultEnum.PARAMS_ERROR, "用户不能重复加入该队伍");
        }

        // 不能加入已经私密的队伍
        TeamStatusEnum statusEnumByValue = TeamStatusEnum.getStatusEnumByValue(team.getStatus());
        if (statusEnumByValue.equals(TeamStatusEnum.PRIVATE)) {
            throw new CommonException(ResultEnum.PARAMS_ERROR, "不能加入私密队伍");
        }
        //加密队伍需要密码验证
        if (statusEnumByValue.equals(TeamStatusEnum.SECRET)) {
            String password = joinTeamParam.getPassword();
            if (StringUtils.isBlank(password)) {
                throw new CommonException(ResultEnum.PARAMS_ERROR, "密码不正确");
            }
            if (!team.getPassword().equals(password)) {
                throw new CommonException(ResultEnum.PARAMS_ERROR, "密码不正确");
            }
        }

        // 新增一条关联信息
        UserTeamRelation userTeamRelation = new UserTeamRelation();
        userTeamRelation.setUserId(userId);
        userTeamRelation.setTeamId(teamId);
        userTeamRelation.setJoinTime(new Date());
        userTeamRelation.setCreateTime(new Date());
        userTeamRelation.setUpdateTime(new Date());
        userTeamRelation.setIsDelete(0);

        int insert = baseMapper.insert(userTeamRelation);

        return insert > 0;
    }
}




